package pl.myszon.RILLib;

import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author Myszon
 */
class AddWebsiteRequest extends Request{

    private String url = "https://readitlaterlist.com/v2/add";
    private Website website = null;

    @Override
    String getRequestURL() throws ConnectionFailureException {
        if(website == null)
            throw new ConnectionFailureException("You should set website url and title");

        if((!isWebsiteCorrect()) || (!isURLCorrect()))
            throw new ConnectionFailureException("You should set website url and title");

       String connectionUrl = url + "?username=" + config.getUsername() + "&password=" + config.getPassword() + "&apikey=" + config.getAPIKey() + "&title=" + website.title + "&url=" + website.url;
        return connectionUrl;
    }

    void setWebsite(Website website) {
        this.website = website;
    }

    private boolean isWebsiteCorrect() {
        if(website.title.isEmpty() || website.url.isEmpty())
            return false;
        else
            return true;
    }

    private boolean isURLCorrect() {
        try {
            URL urlToCheck = new URL(website.url);
            return true;
        } catch (MalformedURLException ex) {
            return false;
        }
    }

}
